package com.frog.service.friend.service.friendList;


import com.frog.framework.cache.redis.FrogRedisClient;
import com.frog.service.friend.cache.ZsetCache;
import com.frog.service.friend.enums.FriendRedisKeys;
import com.frog.service.friend.mapper.FriendFollowingRelationshipDOMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service("blockedStrategy")
public class BlockedStrategy implements ZsetCache, AbstractFriendListStrategy {

    @Autowired
    private FriendFollowingRelationshipDOMapper friendFollowingRelationshipDOMapper;
    @Autowired
    private FrogRedisClient redisClient;


    // AbstractFriendListStrategy

    /**
     * 按时间倒叙查询关注用户
     */
    @Override
    public List<String> list(String userId, long startRow, long endRow) {
        try {
            return new ArrayList<>(readZrevrange( startRow, endRow, FriendRedisKeys.USER_FRIEND_BLOCK_INVITED_ZSET, userId));
        } catch (Exception e) {
            log.error("BlockedStrategy list userId==={}", userId, e);
        }
        return new ArrayList<>();
    }


    /**
     * 关注用户总数
     */
    @Override
    public long total(String userId) {
        try {
            return readZcard( FriendRedisKeys.USER_FRIEND_BLOCK_INVITED_ZSET, userId);
        } catch (Exception e) {
            log.error("BlockedStrategy total userId==={}", userId, e);
        }
        return 0;
    }

    @Override
    public long zrem(String userId, String... members) {
        try {
            return delete( Arrays.asList(members), FriendRedisKeys.USER_FRIEND_BLOCK_INVITED_ZSET, userId);
        } catch (Exception e) {
            log.error("BlockedStrategy zrem userId==={}", userId, e);
        }
        return 0;
    }

    @Override
    public long zadd(String userId, Map<String, Double> scoreMembers) {
        try {
            return write( scoreMembers, FriendRedisKeys.USER_FRIEND_BLOCK_INVITED_ZSET, userId);
        } catch (Exception e) {
            log.error("BlockedStrategy zadd userId==={}", userId, e);
        }
        return 0;
    }


    // ZsetCache

    /**
     * 查询存储层
     */
    @Override
    public Map<String, Double> selectdStorageByKey(Object... keyArgs) {
        Map<String, Double> map = new HashMap<>();
        try {
            if (keyArgs == null || keyArgs.length == 0 || keyArgs[0] == null) return null;
            Integer userId = Integer.parseInt((String) keyArgs[0]);
            List<Integer> list = friendFollowingRelationshipDOMapper.selectBlockInvitedFriendIdListByUserId(userId);
            if (list == null || list.isEmpty()) return map;
            for (Integer f : list) {
                if (f == null) continue;
                map.put(String.valueOf(f), 0D);
            }
        } catch (NumberFormatException e) {
            log.error("BlockedStrategy-selectdStorageByKey userId=={}", keyArgs, e);
        }
        return map;
    }

    /**
     * 添加存储层
     */
    @Override
    public Integer insertStorage(Map<String, Double> members, Object... keyArgs) {
        return 1;
    }

    /**
     * 删除存储层
     */
    @Override
    public Integer deleteStorageByKey(List<String> members, Object... keyArgs) {
        return 1;
    }



}